<template>
  <div class="cart-container">
    <h2>购物车</h2>
    <el-divider />
    
    <div v-if="cartItems.length === 0" class="empty-cart">
      <el-empty description="购物车是空的" />
      <el-button type="primary" @click="goToMenu">去点菜</el-button>
    </div>
    
    <div v-else>
      <el-table :data="cartItems" style="width: 100%">
        <el-table-column label="菜品" width="300">
          <template #default="scope">
            <div class="dish-info">
              <img :src="scope.row.image" class="dish-image" />
              <div>
                <h4>{{ scope.row.name }}</h4>
                <p class="dish-category">{{ scope.row.category }}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="单价" width="120">
          <template #default="scope">
            <span class="dish-price">¥{{ scope.row.price.toFixed(2) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="数量" width="150">
          <template #default="scope">
            <el-input-number 
              v-model="scope.row.quantity" 
              :min="1" 
              :max="20"
              size="small"
              @change="(value) => updateQuantity(scope.row.id, value)"
            />
          </template>
        </el-table-column>
        
        <el-table-column label="小计" width="120">
          <template #default="scope">
            <span class="subtotal">¥{{ (scope.row.price * scope.row.quantity).toFixed(2) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作">
          <template #default="scope">
            <el-button 
              type="danger" 
              size="small" 
              @click="removeFromCart(scope.row.id)"
              :icon="Delete"
              circle
            />
          </template>
        </el-table-column>
      </el-table>
      
      <div class="cart-footer">
        <div class="cart-total">
          <span>总计：</span>
          <span class="total-price">¥{{ totalPrice.toFixed(2) }}</span>
        </div>
        
        <div class="cart-actions">
          <el-button @click="clearCart">清空购物车</el-button>
          <el-button type="primary" @click="checkout">结算</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { cartStore } from '../store/CartStore'
import { Delete } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const store = cartStore()

const cartItems = computed(() => store.getCartItems)
const totalPrice = computed(() => store.getTotalPrice)

const updateQuantity = (id, quantity) => {
  store.updateQuantity(id, quantity)
}

const removeFromCart = (id) => {
  ElMessageBox.confirm(
    '确定要从购物车中移除该菜品吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    store.removeFromCart(id)
  }).catch(() => {})
}

const clearCart = () => {
  ElMessageBox.confirm(
    '确定要清空购物车吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    store.clearCart()
  }).catch(() => {})
}

const goToMenu = () => {
  router.push('/diancai')
}

const checkout = () => {
  ElMessageBox.alert(
    `订单总金额：¥${totalPrice.value.toFixed(2)}`,
    '结算',
    {
      confirmButtonText: '确认支付',
    }
  ).then(() => {
    store.clearCart()
    router.push('/diancai')
  })
}
</script>

<style scoped>
.cart-container {
  padding: 20px;
}

h2 {
  text-align: center;
  margin-bottom: 10px;
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 50px;
}

.dish-info {
  display: flex;
  align-items: center;
}

.dish-image {
  width: 60px;
  height: 60px;
  object-fit: cover;
  margin-right: 15px;
  border-radius: 4px;
}

.dish-info h4 {
  margin: 0 0 5px 0;
  font-size: 16px;
}

.dish-category {
  margin: 0;
  font-size: 12px;
  color: #666;
}

.dish-price {
  color: #666;
}

.subtotal {
  font-weight: bold;
  color: #ff6b6b;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.cart-total {
  font-size: 18px;
}

.total-price {
  font-weight: bold;
  color: #ff6b6b;
  font-size: 24px;
  margin-left: 10px;
}

.cart-actions {
  display: flex;
  gap: 10px;
}
</style>